import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoTabPage extends StatefulWidget {
  const VideoTabPage({super.key});

  @override
  State<VideoTabPage> createState() => _VideoTabPageState();
}

class _VideoTabPageState extends State<VideoTabPage> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold(
        appBar: AppBar(
          bottom: const TabBar(
            tabs: [
              Tab(icon: Icon(Icons.directions_car)),
              Tab(icon: Icon(Icons.directions_transit)),
              Tab(icon: Icon(Icons.directions_bike)),
              Tab(icon: Icon(Icons.dialer_sip)),
            ],
          ),
          title: const Text("Video tabs demo"),
        ),
        body: const TabBarView(
          children: [
            VideoWidget("assets/videos/trailer.mp4"),
            VideoWidget("assets/videos/register.mp4"),
            VideoWidget("assets/videos/trailer.mp4"),
            WebVideoWidget(
              video: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
            )
          ],
        ),
      ),
    );
  }
}

class VideoWidget extends StatefulWidget {
  final String videoAsset;
  const VideoWidget(this.videoAsset, {super.key});

  @override
  State<VideoWidget> createState() => _VideoWidgetState(videoAsset);
}

class _VideoWidgetState extends State<VideoWidget> {
  late VideoPlayerController _controller;

  String videoAsset;
  _VideoWidgetState(this.videoAsset);

  @override
  void initState() {
    super.initState();

    debugPrint("setAndPlayVideo $videoAsset");
    _controller = VideoPlayerController.asset(videoAsset);
    _controller.addListener(() {
      setState(() {});
    });

    _controller.initialize().then((value) {
      _controller.play();
    }).catchError((e) {
      debugPrint("_controller.initialize error: $e");
    });
  }

  @override
  void dispose() {
    debugPrint("dispose $videoAsset");
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Text("播放视频 $videoAsset"),
        const TextField(),
        Expanded(
          child: Container(
            padding: const EdgeInsets.all(20),
            child: Center(
              child: AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: VideoPlayer(_controller),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

class WebVideoWidget extends StatefulWidget {
  final String video;
  const WebVideoWidget({super.key, required this.video});

  @override
  State<WebVideoWidget> createState() => _WebVideoWidgetState(this.video);
}

class _WebVideoWidgetState extends State<WebVideoWidget> {
  final String video;
  late VideoPlayerController _controller;

  _WebVideoWidgetState(this.video);

  @override
  void initState() {
    super.initState();

    debugPrint("setAndPlayVideo $video");
    _controller = VideoPlayerController.networkUrl(Uri.parse(video))
      ..addListener(() {
        setState(() {});
      })
      ..initialize().then((value) {
        _controller.play();
      });
  }

  @override
  void dispose() {
    debugPrint("dispose $video");
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Text("播放视频 $video"),
        Expanded(
          child: Container(
            padding: const EdgeInsets.all(20),
            child: Center(
              child: AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: VideoPlayer(_controller),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
